package com.sample.tdf.sample.service.impl;

import cn.com.taiji.common.dto.PageDTO;
import cn.com.taiji.common.exception.BaseException;
import cn.com.taiji.common.util.CommonBeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sample.tdf.sample.domain.Course;
import com.sample.tdf.sample.domain.Student;
import com.sample.tdf.sample.dto.CourseDTO;
import com.sample.tdf.sample.dto.StudentDTO;
import com.sample.tdf.sample.mapper.StudentMapper;
import com.sample.tdf.sample.service.IStudentService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Override
    public PageDTO getPage(PageDTO pageDTO) throws BaseException {
        return null;
    }



    @Override
    public List<StudentDTO> findByStudentClass(String studentClass) {
        return null;
    }

    @Override
    public String dynamicSql() {
        return null;
    }

    @Override
    public StudentDTO create(StudentDTO dto) {
        Student student = new Student();
        CommonBeanUtil.saveCopy(dto, student);
        save(student);
        CommonBeanUtil.saveCopy(student, dto);
        return dto;
    }

    @Override
    public StudentDTO update(StudentDTO dto) {
        Student student = new Student();
        CommonBeanUtil.saveCopy(dto, student);
        updateById(student);
        CommonBeanUtil.saveCopy(student, dto);
        return dto;
    }

    @Override
    public StudentDTO findByIdOneSelect(String id) {
        Student byStudentId = baseMapper.findByStudentId(id);
        StudentDTO studentDTO = domainListToDto(byStudentId);
        return studentDTO;
    }

    @Override
    public StudentDTO findByIdTwoSelect(String id) {
        Student byIdTwoSelect = baseMapper.findByIdTwoSelect(id);
        StudentDTO studentDTO = domainListToDto(byIdTwoSelect);
        return studentDTO;
    }

    private StudentDTO domainListToDto(Student byIdTwoSelect) {
        StudentDTO studentDTO = new StudentDTO();
        CommonBeanUtil.saveCopy(byIdTwoSelect, studentDTO);
        List<Course> courses = byIdTwoSelect.getCourses();
        List<CourseDTO> courseDTOs = new ArrayList<>();
        for (Course course : courses) {
            CourseDTO courseDTO = new CourseDTO();
            CommonBeanUtil.saveCopy(course, courseDTO);
            courseDTOs.add(courseDTO);
        }
        studentDTO.setCourseDTOS(courseDTOs);
        return studentDTO;
    }
}
